import os
import uuid
from django.core.cache import cache
from rest_framework.generics import ListAPIView
from rest_framework.parsers import MultiPartParser
from rest_framework.response import Response
from rest_framework.views import APIView
from resume import models,resumeser

'''
auth = oss2.Auth(
    access_key_id="LTAI5tKZnvggVhKNXvHRV44Q",
    access_key_secret="sRW7KdKh6JIcLZfEGrrFtPoQHZUJ8A"
)


bucket = oss2.Bucket(auth,"oss-cn-beijing.aliyuncs.com","zsb7")
'''

class Upload(APIView):

    parser_classes = [MultiPartParser]
    def post(self,request):
        file = request.data.get('file',"")
        folder = request.data.get('folder','uploads/')
        if not file:
            return Response({"msg":"请上传文件","code":400})
        file_base,file_ext = os.path.splitext(file.name)
        filename = f"{file_base}_{uuid.uuid4()}{file_ext}"
        oss_file_path = f"{folder}{filename}"
        # bucket.put_object(oss_file_path,file.read())

        return Response({"msg":"上传成功","code":200,"oss_file_path":oss_file_path})

class ResumeList(ListAPIView):
    def get(self,request,*args,**kwargs):
        key = 'resume_list'
        info = cache.get(key)
        if info:
            return Response({"msg":"获取成功","code":200,"data":info})
        else:
            queryset = models.Resume.objects.all()
            ser = resumeser.ResumeSerializer(queryset,many=True)
            cache.set(key,ser.data,60*60*2)
            return Response({"msg":"获取成功","code":200,"data":ser.data})
class ResumeUpdate(APIView):
    def post(self,request):
        id = request.data.get("id")
        info = models.Resume.objects.filter(id=id).first()
        if not info:
            return Response({"msg":"没有该简历","code":400})
        phone = request.data.get("phone")
        email = request.data.get("email")
        # 唯一
        if models.Resume.objects.filter(phone=phone).exclude(id=id).exists():
            return Response({"msg": "该手机号已注册", "code": 400})
        if models.Resume.objects.filter(email=email).exclude(id=id).exists():
            return Response({"msg": "该邮箱已注册", "code": 400})
        ser = resumeser.ResumeSerializer(info,data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"msg":"更新成功","code":200})
        else:
            return Response({"msg":"更新失败","code":400,"error":ser.errors})

class EducatedList(ListAPIView):
    def get(self,request,*args,**kwargs):
        key = 'educated_list'
        info = cache.get(key)
        if info:
            return Response({"msg":"获取成功","code":200,"data":info})
        else:
            queryset = models.Educated.objects.all()
            ser = resumeser.EducatedSerializer(queryset,many=True)
            cache.set(key,ser.data,60*60*2)
            return Response({"msg":"获取成功","code":200,"data":ser.data})
class EducatedAdd(APIView):
    def post(self,request):
        resume = request.data.get("resume")
        # 唯一
        if models.Educated.objects.filter(resume=resume).exists():
            return Response({"msg":"该简历已添加学历信息","code":400})
        ser = resumeser.EducatedSerializer(data=request.data)
        if ser.is_valid():
            ser.save()
            # redis缓存
            key = 'educated_'+str(ser.data.get('id'))
            cache.set(key,ser.data,60*60*2)
            return Response({"msg":"添加成功","code":200})
        else:
            return Response({"msg":"添加失败","code":400,"error":ser.errors})

class EducatedUpdate(APIView):
    def post(self,request):
        id = request.data.get("id")
        info = models.Educated.objects.filter(id=id).first()
        if not info:
            return Response({"msg":"没有该学历信息","code":400})

        resume = request.data.get("resume")
        # 唯一
        if models.Educated.objects.filter(resume=resume).exclude(id=id).exists():
            return Response({"msg": "该简历已添加学历信息", "code": 400})

        ser = resumeser.EducatedSerializer(info,data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"msg":"更新成功","code":200})
        else:
            return Response({"msg":"更新失败","code":400,"error":ser.errors})
